package com.wl.cloud.system.controller;

import com.wl.cloud.core.dto.DataStoreDTO;
import com.wl.cloud.core.dto.RestResultDTO;
import com.wl.cloud.system.service.SysMenuService;
import com.wl.cloud.system.support.dto.SysMenuDTO;
import com.wl.cloud.system.support.dto.SysMenuDistributeDTO;
import com.wl.cloud.system.support.dto.query.SysMenuQueryDTO;
import com.wl.cloud.system.support.utils.ConstantSystem;
import com.wl.cloud.system.support.vo.RouterVO;
import com.wl.cloud.system.support.vo.SysMenuVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜单表 前端控制器
 *
 * @author wanglin
 * @since 2023-09-18
 */
@Tag(name = "菜单表", description = "SysMenuController")
@Validated
@CrossOrigin
@RestController
@RequestMapping(ConstantSystem.PROJECT_URL_PREFIX + "sys/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 分页查询
     *
     * @param pageable
     * @return
     */
    @Operation(summary = "分页")
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<DataStoreDTO<SysMenuVO>> page(@ParameterObject @PageableDefault(sort = "orderIndex", direction = Sort.Direction.ASC) Pageable pageable,
                                                       @ParameterObject SysMenuQueryDTO queryDto) {
        return RestResultDTO.newSuccess(sysMenuService.page(pageable, queryDto));
    }

    /**
     * 列表查询
     *
     * @param sort
     * @return
     */
    @Operation(summary = "列表")
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<List<SysMenuVO>> list(@ParameterObject @SortDefault(sort = "createTime", direction = Sort.Direction.DESC) Sort sort,
                                               @ParameterObject SysMenuQueryDTO queryDto) {
        return RestResultDTO.newSuccess(sysMenuService.list(sort, queryDto));
    }

    /**
     * 保存
     *
     * @param dto
     * @return
     */
    @Operation(summary = "保存")
    @PostMapping("/save")
    public RestResultDTO<Void> save(@Validated @RequestBody SysMenuDTO dto) {
        sysMenuService.save(dto);
        return RestResultDTO.newSuccess();
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改")
    @PostMapping(value = "/update")
    public RestResultDTO<Void> update(@Validated @RequestBody SysMenuDTO dto) {
        sysMenuService.update(dto);
        return RestResultDTO.newSuccess();
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询详情")
    @GetMapping("/get")
    public RestResultDTO<SysMenuVO> get(@Parameter(description = "记录ID") @RequestParam String id) {
        return RestResultDTO.newSuccess(sysMenuService.get(id));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.GET})
    public RestResultDTO<Void> delete(@RequestParam Set<String> ids) {
        sysMenuService.delete(ids);
        return RestResultDTO.newSuccess(null, "删除成功");
    }

    @Operation(summary = "树形菜单列表")
    @RequestMapping(value = "treeMenuList", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<List<SysMenuVO>> treeMenuList(@ParameterObject @SortDefault(sort = "level", direction = Sort.Direction.ASC) Sort sort,
                                                       @ParameterObject SysMenuQueryDTO queryDto) {
        return RestResultDTO.newSuccess(sysMenuService.treeMenuList(sort, queryDto));
    }

    @Operation(summary = "根据角色id获取树形菜单列表")
    @RequestMapping(value = "treeMenuListByRoleId", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<List<SysMenuVO>> treeMenuListByRoleId(@Parameter(description = "角色ID") @RequestParam String roleId) {
        return RestResultDTO.newSuccess(sysMenuService.treeMenuListByRoleId(roleId));
    }

    @Operation(summary = "根据角色id分配菜单权限")
    @RequestMapping(value = "distributeMenu", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<Void> distributeMenu(@Parameter(description = "入参") @Validated @RequestBody SysMenuDistributeDTO dto) {
        sysMenuService.distributeMenu(dto);
        return RestResultDTO.newSuccess(null, "操作成功");
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @Operation(summary = "根据用户账号userID，获取路由信息")
    @RequestMapping(value = "getRouters", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<List<RouterVO>> getRouters(@Parameter(description = "用户账号ID") @RequestParam String userId) {
        List<SysMenuVO> menuVoList = sysMenuService.selectMenuTreeByUserId(userId);
        return RestResultDTO.newSuccess(sysMenuService.buildMenus(menuVoList));
    }
}
